package com.wuxiaofan.secondkill.api;

import com.wuxiaofan.secondkill.entity.User;
import com.wuxiaofan.secondkill.service.UserService;
import com.wuxiaofan.secondkill.utils.Md5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class LoginApi {

    @Resource
    private UserService userService;

    @RequestMapping({"/", "/toLogin"})
    public String toLogin() {
        return "customlogin.html";
    }

    @RequestMapping({"/toGoodsList"})
    public String toGoodsList() {
        return "goodsList.html";
    }

    @RequestMapping({"/toDetail"})
    public String toDetail(Model model, String id) {
        model.addAttribute("id", id);
        return "goodsDetail.html";
    }


    @RequestMapping("login")
    public String login(Long id, String passwd, Model model) {
        User tmp = userService.findById(id);
        if (tmp != null) {
            if (Md5Util.encode(passwd, tmp.getSalt()).equals(tmp.getPasswd())) {
                model.addAttribute("user", tmp);
                return "index.html";
            }
            model.addAttribute("msg", "用户名密码错误!");
            return "customlogin.html";
        }
        model.addAttribute("msg", "用户不存在!");
        return "customlogin.html";
    }

}
